package view.util;



public class JNumberBox extends JFieldBox {

	/**
	 *  JNumberBox - For dealing with real numbers
	 */
	private static final long serialVersionUID = 1L;

	public JNumberBox(String caption, double value) {
		super(caption, Double.toString(value));
	}

	private double minValue = Double.NEGATIVE_INFINITY;
	private double maxValue = Double.POSITIVE_INFINITY;

	public boolean checkTextFormat(String S) {
		try {
			double	value = Double.parseDouble(S);
			return (value >= getMinValue() && value < getMaxValue());
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public double	getValue() {
		try {
			return Double.parseDouble(getText());
		} catch (NumberFormatException e) {
			return Double.parseDouble(oldText);
		}
	}

	public void setValue(double value) {
		setText(Double.toString(value));
	}

	public double getMinValue() {
		return minValue;
	}

	public void setMinValue(double minValue) {
		this.minValue = Math.min(minValue, maxValue);
	}

	public double getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(double maxValue) {
		this.maxValue = Math.max(minValue, maxValue);
	}

}
